package org.example.student_dormitory.dao.impl;

import org.example.student_dormitory.dao.AdminManagerDao;
import org.example.student_dormitory.entity.Admin;
import org.example.student_dormitory.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class AdminManagerDaoImpl implements AdminManagerDao {

    @Override
    public List<Admin> selectAll(int page, int pageSize) {
        //创建一个查询结果集合
        List<Admin> list = new ArrayList<>();
        String sql = "select * from admin where dr=1 or dr=0 order by id desc limit ?,?";
        page = (page-1) * pageSize;
        List<Map<String, Object>> queryList = DBUtils.queryList(sql, page, pageSize);

//        System.out.println("即将在Dao中进行判断");
        if (queryList == null) {
            System.out.println("null");

            return null;
        }else {
            //获取总数据条数
            int count = selectCount();
            int pagetotal = count % pageSize == 0 ? count / pageSize : count /pageSize + 1;
            for (int i = 0; i < queryList.size(); i++) {
                Admin admin = new Admin();
                admin.setId(Long.parseLong(queryList.get(i).get("id").toString()));
                admin.setUsername(queryList.get(i).get("username").toString());
                admin.setPassword(queryList.get(i).get("password").toString());
                admin.setGender(queryList.get(i).get("gender").toString());
                admin.setName(queryList.get(i).get("name").toString());
                admin.setPhone(queryList.get(i).get("phone").toString());
                admin.setDr(Long.parseLong(queryList.get(i).get("dr").toString()));
                admin.setPageTotal(count);//总数居条数
                //添加至对象
                list.add(admin);
            }
//            System.out.println("返回admin对象");
            return list;
        }

    }

    //根据关键字分页查询
    @Override
    public List<Admin> selectAll(int page, int pageSize, String key, String value) {
        //创建一个查询结果集合
        List<Admin> list = new ArrayList<>();
        String sql = "select * from admin where "+key+" like ? and (dr=1 or dr=0) order by id desc limit ?,?";
        //计算分页数据
        page = (page-1) * pageSize;

        //拼接模糊查询的关键字
        value = "%" + value + "%";
        List<Map<String, Object>> queryList = DBUtils.queryList(sql, value, page, pageSize);
        if (queryList == null) {
            return null;
        }else {
            //获取总数据条数
            String sql1 = "select count(*) from admin where "+key+" like ? and (dr=1 or dr=0)";
            Map<String, Object> stringObjectMap = DBUtils.qureyOne(sql1, value);
            int count = Integer.parseInt(stringObjectMap.get("count(*)").toString());
            //根据总数计算出总页数
            for (int i = 0; i < queryList.size(); i++) {
                Admin admin = new Admin();
                admin.setId(Long.parseLong(queryList.get(i).get("id").toString()));
                admin.setUsername(queryList.get(i).get("username").toString());
                admin.setPassword(queryList.get(i).get("password").toString());
                admin.setGender(queryList.get(i).get("gender").toString());
                admin.setName(queryList.get(i).get("name").toString());
                admin.setPhone(queryList.get(i).get("phone").toString());
                admin.setDr(Long.parseLong(queryList.get(i).get("dr").toString()));
                admin.setPageTotal(count);//总数居条数
                //添加至对象
                list.add(admin);
            }
            return list;
        }

    }

    @Override
    public int selectCount() {
        String sql = "select count(*) from admin where dr=1 or dr=0";
        Map<String, Object> stringObjectMap = DBUtils.qureyOne(sql);
        return Integer.parseInt(stringObjectMap.get("count(*)").toString());
    }

    //根据用户名判断
    @Override
    public Boolean selectByUsername(String username) {
        String sql = "select * from admin where username =?";
        Map<String, Object> adminMap = DBUtils.qureyOne(sql, username);
        if (adminMap == null) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Boolean insert(Admin admin) {
        String sql = "insert into admin (username, password, name, gender, phone, dr) values(?,?,?,?,?,?)";
        try {
            return DBUtils.insert(sql, admin.getUsername(), admin.getPassword(), admin.getName(), admin.getGender(), admin.getPhone(), admin.getDr());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Boolean deleteById(int id) {
        String sql = "update admin set dr=2 where id=?";
        try {
            return DBUtils.update(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public boolean update(Admin admin) {
        String sql = "update admin set username=?, password=?, name=?, gender=?, phone=?, dr=? where id=?";
        try {
//            System.out.println("在Dao中准备返回结果");
            return DBUtils.update(sql, admin.getUsername(), admin.getPassword(), admin.getName(), admin.getGender(), admin.getPhone(), admin.getDr(), admin.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteList(int[] ids) {
        String s = "";
        for (int i = 0; i < ids.length; i++) {
            s += "?";
            if (i != ids.length-1){
                s += ",";
            }
        }
        String sql = "update admin set dr=2 where id in (" + s + ")";
        //获取数据连接
        Connection conn = null;
        PreparedStatement ps = null;
        try{
            conn = DBUtils.getConn();
            ps = conn.prepareStatement(sql);
            //动态
            for (int i = 0; i < ids.length; i++) {
                ps.setInt(i+1,ids[i]);
            }
            int i =  ps.executeUpdate();
            if (i > 0){
                return true;
            }else {
                return false;
            }
        } catch (Exception e){
            throw new RuntimeException(e);
        } finally {
            DBUtils.close(null,ps,conn);
        }
    }
}
